﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Timer;
using Hi_Baidu.Entry.CollectionObject;

namespace Hi_Baidu
{
    public partial  class Win_Main : Window
    {
        private void txt_NotifyNickName_LostFocus(object sender, RoutedEventArgs e)
        {
            this.UpdateProd();
        }

        /// <summary>
        ///     内部更新个性签名方法
        /// </summary>
        private void UpdateProd()
        {
            this.tb_UserProd.Visibility = Visibility.Visible;
            this.txt_NotifyNickName.Visibility = Visibility.Hidden;
            //个性签名有变动
            if (this.txt_NotifyNickName.Text != this.tb_UserProd.Text)
            {
                //向服务器发送更改个性签名请求
                UpdateProcessModel.ModifyProd(Globle.user.DIC_USERINFO["USERNAME"], Encoding.Default.GetBytes(this.txt_NotifyNickName.Text));
                //安装修改个性签名超时器
                ModifyProdTimeOutEvent modifyProdTimeOutEvent = new ModifyProdTimeOutEvent(
                    Globle.user.DIC_USERINFO["USERNAME"],
                    Encoding.Default.GetBytes(this.txt_NotifyNickName.Text),
                    "ModifyProdTimeOut",
                    2000,
                    2);
                Globle.timeoutTimerManager.InstallTimer(modifyProdTimeOutEvent);
            }
        }

        private void tb_UserProd_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.tb_UserProd.Visibility = Visibility.Hidden;
            this.txt_NotifyNickName.Visibility = Visibility.Visible;
            this.txt_NotifyNickName.Focus();
            this.txt_NotifyNickName.Text = this.tb_UserProd.Text;
        }

        private void txt_NotifyNickName_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Return)
            {
                this.UpdateProd();
            }
        }

        private void Img_UserHead_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("123");
        }

        private void Img_UserHead_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //当前窗口集合中未存在该窗口
            if(!Globle.popWindowManager.ActiveWindow("ModifyUserInfo"))
            {
                Win_ModifyUserInfo win_ModifyUserInfo = new Win_ModifyUserInfo("ModifyUserInfo");
                Globle.popWindowManager.Add(win_ModifyUserInfo);
                win_ModifyUserInfo.Show();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            LoginProcessModel.Logout();
            Environment.Exit(0);
        }

        private void TV_FriendList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = this.TV_FriendList.SelectedItem as TreeViewItem;
            if (treeViewItem != null && treeViewItem.Tag is Friend)
            {
                if (!Globle.popWindowManager.Equals((treeViewItem.Tag as Friend).Username))
                {
                    Win_TalkWindow win_TalkWindow = new Win_TalkWindow((treeViewItem.Tag as Friend).Username, (treeViewItem.Tag as Friend));
                    Globle.popWindowManager.Add(win_TalkWindow);
                    win_TalkWindow.Show();
                }
                else
                {
                    Globle.popWindowManager.ActiveWindow((treeViewItem.Tag as Friend).Username);
                }
            }
        }

        private void txt_NotifyNickName_MouseDown(object sender, MouseButtonEventArgs e)
        {
            
        }
    }
}
